package com.commander.gui;

import java.sql.SQLException;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.commander.Commander;
import com.commander.gui.messages.ErrorMessage;

public class ConnectTab extends Tab{	
	Composite tabFolderPage;
	SashForm sash;
	
	Group loginGroup;
	Label unLabel;
	Text username;
	Label pwLabel;
	Text password;
	Button login;
	
	Group dbSelectGroup;
	List availableDBs;
	Button selectDB;
	
	public ConnectTab(TabFolder tabFolder){
		initialize(tabFolder);
	}
	
	private void initialize(TabFolder tabFolder){
		tabFolderPage = new Composite(tabFolder, SWT.BORDER);
		tabFolderPage.setToolTipText("Composite");
		createLoginGroup();
		createDBSelectionGroup();
	}
	
	private void createLoginGroup(){
		loginGroup = new Group(tabFolderPage, SWT.NONE);
		loginGroup.setLayout(new GridLayout());
		loginGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		loginGroup.setText("Login");
		//sash = new SashForm(loginGroup, SWT.SMOOTH);
		unLabel = new Label(loginGroup, SWT.NONE);
		unLabel.setText("Username:");
		username = new Text(loginGroup, SWT.BORDER | SWT.SINGLE);
		username.setSize(100, 20);
		pwLabel = new Label(loginGroup, SWT.NONE);
		pwLabel.setText("Password:");
		password = new Text(loginGroup, SWT.BORDER | SWT.PASSWORD);
		password.setSize(100, 20);
		login = new Button(loginGroup, SWT.NONE);
		login.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				if(username.getText() != null && password.getText() != null){
					Commander.getCommander().login(username.getText(), password.getText());
					if(Commander.getCommander().getConnection() != null){
						try {
							if(!Commander.getCommander().getConnection().isClosed())
								availableDBs.setItems(Commander.getCommander().getAvailableDBs());
						} catch (SQLException e1) {
							new ErrorMessage(e1.getMessage());
						}
					}
				}
			}
		});
	}
	
	private void createDBSelectionGroup(){
		dbSelectGroup = new Group(tabFolderPage, SWT.NONE);
		dbSelectGroup.setLayout(new GridLayout());
		dbSelectGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		dbSelectGroup.setText("Available Databases");
		availableDBs = new List(dbSelectGroup, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		
	}
}